package UILayer;

import CtrLayer.CustomerCtr;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class GroupsUI extends JInternalFrame
{
	private JTextField text_field;
	private CustomerCtr group_ctr;
	private JTable group_table;
	Object[] column_names = { "Name", "Terms of Payment", "Discount %" };

	/**
	 * Create the frame.
	 */
	public GroupsUI()
	{

		super("Manage Groups", true, true, true, true);
		getContentPane().setForeground(new Color(255, 153, 0));
		this.addInternalFrameListener(new InternalFrameAdapter()
		{
			public void internalFrameActivated(InternalFrameEvent e)
			{
				refresh();
			}
		});
		group_ctr = new CustomerCtr();
		fill_frame();

	}

	private void refresh()
	{
		refill_full_table();

	}

	private void refill_full_table()
	{
		Object[][] filling = group_ctr.display_all_groups();
		group_table.setModel(new DefaultTableModel(filling, column_names));

	}

	private void make_table()
	{

		Object[][] filling = group_ctr.display_all_groups();
		JPanel ctr_btn_panel = new JPanel();
		ctr_btn_panel.setBackground(new Color(255, 153, 0));
		ctr_btn_panel.setBorder(new EmptyBorder(3, 3, 0, 3));

		getContentPane().add(ctr_btn_panel, BorderLayout.SOUTH);
		ctr_btn_panel.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.setBackground(new Color(255, 153, 0));
		ctr_btn_panel.add(panel, BorderLayout.EAST);

		JButton btn_new = new JButton("New");
		panel.add(btn_new);
		btn_new.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				add();
			}
		});

		JButton btn_edit = new JButton("Edit");
		panel.add(btn_edit);
		btn_edit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				edit();
			}
		});

		JButton btn_del = new JButton("Delete");
		panel.add(btn_del);
		btn_del.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				delete();
			}
		});

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(255, 153, 0));
		ctr_btn_panel.add(panel_1, BorderLayout.WEST);

		JPanel panel_2 = new JPanel();
		getContentPane().add(panel_2, BorderLayout.CENTER);

		group_table = new JTable()
		{
			public boolean isCellEditable(int row, int column)
			{
				return false;
			}
		};
		group_table.addKeyListener(new KeyAdapter()
		{
			public void keyTyped(KeyEvent e)
			{
				if (e.getKeyChar() == '\u007F')
				{
					delete();
				}
			}
		});
		group_table.setForeground(Color.WHITE);
		group_table.setBackground(new Color(127, 127, 127));
		group_table.setFillsViewportHeight(true);
		group_table.setModel(new DefaultTableModel(filling, column_names));

		group_table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		group_table.setAutoCreateRowSorter(true);
		group_table.setRowSelectionAllowed(true);
		group_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		group_table.getColumnModel().getColumn(2).setMinWidth(160);
		group_table.getTableHeader().setReorderingAllowed(false);

		JScrollPane scroll_pane = new JScrollPane(group_table);
		scroll_pane.setBorder(new EmptyBorder(5, 10, 5, 10));
		getContentPane().add(scroll_pane, BorderLayout.WEST);
	}

	private void make_ctr_buttons()
	{

	}

	private void make_search(JPanel panel)
	{
		JPanel search_panel = new JPanel();
		search_panel.setBorder(null);
		search_panel.setBackground(new Color(255, 153, 0));
		panel.add(search_panel, BorderLayout.WEST);
		search_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JLabel lblNewLabel = new JLabel("Name");
		search_panel.add(lblNewLabel);

		text_field = new JTextField();
		search_panel.add(text_field);
		text_field.setColumns(20);

		JButton btn_search = new JButton("Search");
		btn_search.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				search();
			}
		});
		search_panel.add(btn_search);

		JButton btnClear = new JButton("Clear");
		btnClear.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				refresh();
				text_field.setText("");
			}
		});
		search_panel.add(btnClear);

	}

	private void add()
	{
		GroupManager p = new GroupManager(null);
		getDesktopPane().add(p);
		p.setVisible(true);
	}

	private void edit()
	{
		int row_index = group_table.getSelectedRow();
		if (row_index != -1)
		{
			if( group_table.getModel().getValueAt(row_index, 0) != null)
			{
				GroupManager p = new GroupManager((String) group_table.getModel()
						.getValueAt(row_index, 0));
				getDesktopPane().add(p);
				p.setVisible(true);
			}
		}
	}

	private void delete()
	{
		int row_index = group_table.getSelectedRow();
		if (row_index != -1)
		{
			if( group_table.getModel().getValueAt(row_index, 0) != null)
			{
				if (group_ctr.is_group_used((String) group_table.getModel()
						.getValueAt(row_index, 0)))
				{
					JOptionPane
							.showMessageDialog(
									this,
									"Couldn't delete a group.\nThere are clients assigned to it.\nMove them to another one to delete the group",
									"Error", JOptionPane.INFORMATION_MESSAGE);
				}
				else if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(
						this, "Are you sure you want to delete this group?",
						"Delete group", JOptionPane.YES_NO_OPTION))
				{
					group_ctr.delete_group((String) group_table.getModel()
							.getValueAt(row_index, 0));
					refresh();
				}
			}
		}

	}

	private void search()
	{
		Object[][] filling = new Object[1][5];
		String contents = text_field.getText();

		filling[0] = group_ctr.display_group(contents);
		group_table.setModel(new DefaultTableModel(filling, column_names));
	}

	private void fill_frame()
	{
		setBounds(120, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout(0, 0));
		JPanel panel = new JPanel();
		panel.setBackground(new Color(255, 153, 0));
		panel.setBorder(null);
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		make_search(panel);
		make_ctr_buttons();
		make_table();
	}

}
